#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 颜色定义
#define RED 1
#define GREEN 2
#define YELLOW 3
#define BLUE 4
#define WHITE 7
#define BLACK   0
// 农历数据：1900-2100年的农历信息
const int lunarInfo[201] = {
    0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
    0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
    0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
    0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
    0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
    0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
    0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
    0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
    0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
    0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
    0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
    0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
    0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
    0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
    0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0
};

// 天干地支数组
const char* heavenlyStems[] = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
const char* earthlyBranches[] = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
const char* animalZodiac[] = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};

// 节气名称
const char* solarTerms[] = {"小寒", "大寒", "立春", "雨水", "惊蛰", "春分", 
                           "清明", "谷雨", "立夏", "小满", "芒种", "夏至", 
                           "小暑", "大暑", "立秋", "处暑", "白露", "秋分", 
                           "寒露", "霜降", "立冬", "小雪", "大雪", "冬至"};

// 公历节日
const char* solarHolidays[13][5] = {
    {NULL},
    {"元旦", "1月1日", NULL, NULL, NULL},
    {"情人节", "2月14日", "除夕", NULL, NULL},
    {"妇女节", "3月8日", "植树节", "3月12日", NULL},
    {"愚人节", "4月1日", "清明节", "4月5日", NULL},
    {"劳动节", "5月1日", "青年节", "5月4日", NULL},
    {"儿童节", "6月1日", NULL, NULL, NULL},
    {"建党节", "7月1日", NULL, NULL, NULL},
    {"建军节", "8月1日", NULL, NULL, NULL},
    {"教师节", "9月10日", NULL, NULL, NULL},
    {"国庆节", "10月1日", NULL, NULL, NULL},
    {"光棍节", "11月11日", NULL, NULL, NULL},
    {"圣诞节", "12月25日", NULL, NULL, NULL}
};

// 农历节日
const char* lunarHolidays[][3] = {
    {"春节", "正月初一"},
    {"元宵节", "正月十五"},
    {"端午节", "五月初五"},
    {"七夕节", "七月初七"},
    {"中秋节", "八月十五"},
    {"重阳节", "九月初九"},
    {"腊八节", "腊月初八"},
    {"小年", "腊月廿三"},
    {NULL, NULL}
};

// 日期结构体
typedef struct {
    int year;
    int month;
    int day;
    int isLunar;  // 0-公历，1-农历
} Date;

// 事件结构体
typedef struct {
    Date date;
    char description[100];
} Event;

Event events[100];
int eventCount = 0;

// Windows控制台颜色设置
#ifdef _WIN32
#include <windows.h>
void setColor(int foreground, int background) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, (background << 4) | foreground);
}
#else
// Linux/Mac颜色设置
void setColor(int foreground, int background) {
    printf("\033[%d;%dm", foreground + 30, background + 40);
}
#endif

// 重置颜色
void resetColor() {
    #ifdef _WIN32
    setColor(7, 0);
    #else
    printf("\033[0m");
    #endif
}

// 获取当前时间
void getCurTime(Date* date) {
    time_t t = time(NULL);
    struct tm* now = localtime(&t);
    date->year = now->tm_year + 1900;
    date->month = now->tm_mon + 1;
    date->day = now->tm_mday;
    date->isLunar = 0;
}

// 判断是否为闰年（公历）
int isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// 获取公历月份天数
int getSolarDays(int year, int month) {
    int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    return (month == 2 && isLeapYear(year)) ? 29 : days[month - 1];
}

// 获取农历月份天数
int getLunarDays(int lunarYear, int lunarMonth, int isLeap) {
    if (isLeap) {
        return (lunarInfo[lunarYear - 1900] & (0x10000 >> lunarMonth)) ? 30 : 29;
    } else {
        return (lunarInfo[lunarYear - 1900] & (0x10000 >> lunarMonth)) ? 30 : 29;
    }
}

// 获取农历闰月
int getLeapMonth(int lunarYear) {
    return lunarInfo[lunarYear - 1900] & 0xf;
}

// 农历转公历
Date lunarToSolar(int lunarYear, int lunarMonth, int lunarDay, int isLeap) {
    Date solar = {0};
    int i, days = 0;
    
    // 计算从1900年到目标农历年的总天数
    for (i = 1900; i < lunarYear; i++) {
        days += 348;  // 平均每年348天
        for (int m = 1; m <= 12; m++) {
            if (lunarInfo[i - 1900] & (0x10000 >> m)) {
                days++;  // 大月+1天
            }
        }
        if (getLeapMonth(i) > 0) {
            days += (lunarInfo[i - 1900] & (0x10000 >> getLeapMonth(i))) ? 30 : 29;
        }
    }
    
    // 计算目标农历年中到目标月份的天数
    for (i = 1; i < lunarMonth; i++) {
        days += getLunarDays(lunarYear, i, 0);
    }
    
    // 如果是闰月，加上闰月的天数
    if (isLeap) {
        days += getLunarDays(lunarYear, lunarMonth, 1);
    }
    
    // 加上当月的天数
    days += lunarDay;
    
    // 从1900-01-31开始计算公历日期
    solar.year = 1900;
    solar.month = 1;
    solar.day = 31;
    
    // 逐年增加，直到天数不足一年
    while (days > 0) {
        int yearDays = 365;
        if (isLeapYear(solar.year)) {
            yearDays = 366;
        }
        
        if (days >= yearDays) {
            days -= yearDays;
            solar.year++;
        } else {
            // 逐月增加
            for (i = 1; i <= 12 && days > 0; i++) {
                int monthDays = getSolarDays(solar.year, i);
                if (days >= monthDays) {
                    days -= monthDays;
                } else {
                    solar.month = i;
                    solar.day += days;
                    days = 0;
                    break;
                }
            }
        }
    }
    
    return solar;
}

// 公历转农历
void solarToLunar(int solarYear, int solarMonth, int solarDay, int* lunarYear, 
                  int* lunarMonth, int* lunarDay, int* isLeap) {
    int i, leap = 0, temp = 0;
    int offset = 0;
    
    // 计算从1900-01-31到目标日期的天数
    for (i = 1900; i < solarYear; i++) {
        offset += 365;
        if (isLeapYear(i)) {
            offset += 1;
        }
    }
    
    // 加上当年到当月的天数
    for (i = 1; i < solarMonth; i++) {
        offset += getSolarDays(solarYear, i);
    }
    
    offset += solarDay - 31;  // 减去1900-01-31的天数
    
    // 查找农历年份
    *lunarYear = 1900;
    while (offset > 0) {
        int yearDays = 348;
        leap = getLeapMonth(*lunarYear);
        
        // 计算当年农历天数
        for (i = 1; i <= 12; i++) {
            yearDays += (lunarInfo[*lunarYear - 1900] & (0x10000 >> i)) ? 30 : 29;
        }
        
        // 如果有闰月，加上闰月天数
        if (leap > 0) {
            yearDays += (lunarInfo[*lunarYear - 1900] & (0x10000 >> leap)) ? 30 : 29;
        }
        
        if (offset >= yearDays) {
            offset -= yearDays;
            (*lunarYear)++;
        } else {
            // 计算农历月份
            *isLeap = 0;
            for (i = 1; i <= 12; i++) {
                temp = (lunarInfo[*lunarYear - 1900] & (0x10000 >> i)) ? 30 : 29;
                
                // 如果是闰月，且当前月份是闰月
                if (leap > 0 && i == leap && *isLeap == 0) {
                    (*lunarMonth) = i;
                    *isLeap = 1;
                    temp = (lunarInfo[*lunarYear - 1900] & (0x10000 >> leap)) ? 30 : 29;
                    
                    if (offset >= temp) {
                        offset -= temp;
                    } else {
                        *lunarDay = offset;
                        return;
                    }
                } else if (*isLeap == 1 && i == (leap + 1)) {
                    *isLeap = 0;
                }
                
                (*lunarMonth) = i;
                if (offset >= temp) {
                    offset -= temp;
                } else {
                    *lunarDay = offset;
                    return;
                }
            }
            
            // 如果还有剩余天数，说明是闰月的下一个月
            if (leap > 0 && offset > 0) {
                *lunarMonth = leap;
                *isLeap = 1;
                *lunarDay = offset;
            }
        }
    }
}

// 获取天干地支年
char* getChineseYear(int year) {
    static char result[20];
    int stem = (year - 4) % 10;
    int branch = (year - 4) % 12;
    int zodiac = branch;
    sprintf(result, "%s%s年【%s】", heavenlyStems[stem], earthlyBranches[branch], animalZodiac[zodiac]);
    return result;
}

// 获取农历月份名称
char* getLunarMonthName(int lunarMonth, int isLeap) {
    static char result[10];
    const char* monthNames[] = {"", "正月", "二月", "三月", "四月", "五月", "六月", 
                              "七月", "八月", "九月", "十月", "冬月", "腊月"};
    
    if (isLeap) {
        sprintf(result, "闰%s", monthNames[lunarMonth]);
    } else {
        strcpy(result, monthNames[lunarMonth]);
    }
    
    return result;
}

// 获取农历日名称
char* getLunarDayName(int lunarDay) {
    static char result[10];
    const char* tenDayNames[] = {"初", "十", "廿", "卅"};
    const char* dayNames[] = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
    
    if (lunarDay == 10) {
        strcpy(result, "初十");
    } else if (lunarDay == 20) {
        strcpy(result, "二十");
    } else if (lunarDay == 30) {
        strcpy(result, "三十");
    } else {
        sprintf(result, "%s%s", tenDayNames[lunarDay / 10], dayNames[lunarDay % 10 - 1]);
    }
    
    return result;
}

// 获取星期几
int getWeekday(int year, int month, int day) {
    if (month < 3) {
        month += 12;
        year--;
    }
    int c = year / 100;
    year = year % 100;
    int w = (c / 4 - 2 * c + year + year / 4 + 13 * (month + 1) / 5 + day - 1) % 7;
    if ((w + 7) % 7==0){
        return 7
    }
    else{
        return (w + 7) % 7;
    }
}


// 节气计算（简化版）
int getSolarTermDay(int year, int termIndex) {
    // 这里使用简化的节气计算，实际应用中应使用更精确的天文算法
    double c[] = {6.11, 20.84, 4.6295, 19.4599, 6.3826, 21.4155, 5.59, 20.888, 
                  6.318, 21.86, 6.5, 22.2, 7.928, 23.65, 8.35, 23.95, 
                  8.44, 23.822, 9.098, 24.218, 8.218, 23.08, 7.9, 22.6};
    
    int y = year % 100;
    double d = 0.2422 * y + c[termIndex];
    
    if (termIndex == 0 || termIndex == 1) {  // 小寒、大寒
        if (isLeapYear(year)) {
            d -= 1;
        } else {
            d -= 2;
        }
    } else if (termIndex == 2 || termIndex == 3) {  // 立春、雨水
        if (isLeapYear(year)) {
            d -= 1;
        }
    }
    
    return (int)d;
}

// 判断是否为节气
int isSolarTerm(int year, int month, int day, char* termName) {
    int term1, term2;
    int termIndex1 = (month - 1) * 2;
    int termIndex2 = termIndex1 + 1;
    
    term1 = getSolarTermDay(year, termIndex1);
    term2 = getSolarTermDay(year, termIndex2);
    
    if (day == term1) {
        strcpy(termName, solarTerms[termIndex1]);
        return 1;
    } else if (day == term2) {
        strcpy(termName, solarTerms[termIndex2]);
        return 1;
    }
    
    return 0;
}

// 获取下一个节气
void getNextSolarTerm(int year, int month, int day, char* termName, int* nextTermDay) {
    int term1, term2;
    int termIndex1 = (month - 1) * 2;
    int termIndex2 = termIndex1 + 1;
    
    term1 = getSolarTermDay(year, termIndex1);
    term2 = getSolarTermDay(year, termIndex2);
    
    if (day < term1) {
        strcpy(termName, solarTerms[termIndex1]);
        *nextTermDay = term1;
    } else if (day < term2) {
        strcpy(termName, solarTerms[termIndex2]);
        *nextTermDay = term2;
    } else {
        // 下个月的第一个节气
        int nextMonth = month % 12 + 1;
        int nextYear = year;
        if (nextMonth == 1) {
            nextYear++;
        }
        
        int nextTermIndex = (nextMonth - 1) * 2;
        strcpy(termName, solarTerms[nextTermIndex]);
        *nextTermDay = getSolarTermDay(nextYear, nextTermIndex);
        if (nextMonth == 1) {
            *nextTermDay += 365 + (isLeapYear(year) ? 1 : 0);
        }
    }
}

// 计算两个日期之间的天数差
int getDaysBetween(Date d1, Date d2) {
    // 转换为公历天数计算
    if (d1.isLunar) {
        Date temp = lunarToSolar(d1.year, d1.month, d1.day, 0);
        d1 = temp;
    }
    
    if (d2.isLunar) {
        Date temp = lunarToSolar(d2.year, d2.month, d2.day, 0);
        d2 = temp;
    }
    
    // 计算从1900-01-01到各日期的天数
    int days1 = 0, days2 = 0;
    int i;
    
    // 计算d1的天数
    for (i = 1900; i < d1.year; i++) {
        days1 += 365;
        if (isLeapYear(i)) {
            days1 += 1;
        }
    }
    
    for (i = 1; i < d1.month; i++) {
        days1 += getSolarDays(d1.year, i);
    }
    
    days1 += d1.day;
    
    // 计算d2的天数
    for (i = 1900; i < d2.year; i++) {
        days2 += 365;
        if (isLeapYear(i)) {
            days2 += 1;
        }
    }
    
    for (i = 1; i < d2.month; i++) {
        days2 += getSolarDays(d2.year, i);
    }
    
    days2 += d2.day;
    
    return days2 - days1;
}

// 计算距今天n天的日期
Date getDateAfterDays(int days) {
    Date today, result;
    getCurTime(&today);
    
    result = today;
    result.day += days;
    
    // 调整日期
    while (result.day > getSolarDays(result.year, result.month)) {
        result.day -= getSolarDays(result.year, result.month);
        result.month++;
        
        if (result.month > 12) {
            result.month = 1;
            result.year++;
        }
    }
    
    while (result.day <= 0) {
        result.month--;
        
        if (result.month < 1) {
            result.month = 12;
            result.year--;
        }
        
        result.day += getSolarDays(result.year, result.month);
    }
    
    return result;
}

// 添加事件
void addEvent() {
    if (eventCount >= 100) {
        printf("事件列表已满！\n");
        return;
    }
    
    Date date;
    printf("请输入日期（年 月 日，公历）：");
    scanf("%d %d %d", &date.year, &date.month, &date.day);
    date.isLunar = 0;
    
    printf("请输入事件描述：");
    scanf(" %[^\n]s", ev ents[eventCount].description);
    
    events[eventCount].date = date;
    eventCount++;
    
    printf("事件添加成功！\n");
}

// 查看指定日期的事件
void viewEvents() {
    Date date;
    printf("请输入要查看的日期（年 月 日，公历）：");
    scanf("%d %d %d", &date.year, &date.month, &date.day);
    date.isLunar = 0;
    
    int found = 0;
    printf("\n%d年%d月%d日的事件：\n", date.year, date.month, date.day);
    
    for (int i = 0; i < eventCount; i++) {
        if (events[i].date.year == date.year && 
            events[i].date.month == date.month && 
            events[i].date.day == date.day) {
            printf(" - %s\n", events[i].description);
            found = 1;
        }
    }
    
    if (!found) {
        printf("没有找到事件！\n");
    }
}

// 显示主菜单
void showMainMenu() {
    Date today;
    getCurTime(&today);
    
    #ifdef _WIN32
    system("cls");
    #else
    system("clear");
    #endif
    
    setColor(RED, WHITE);
    printf("\n\t\t===== 超级万年历 =====\n");
    resetColor();
    
    setColor(GREEN, BLACK);
    printf("\t当前时间: %d年%d月%d日 星期%d\n", 
           today.year, today.month, today.day, getWeekday(today.year, today.month, today.day));
    resetColor();
    
    // 显示农历信息
    int lunarYear, lunarMonth, lunarDay, isLeap;
    solarToLunar(today.year, today.month, today.day, &lunarYear, &lunarMonth, &lunarDay, &isLeap);
    
    setColor(YELLOW, BLACK);
    printf("\t农历: %s %s %s\n", 
           getChineseYear(lunarYear), 
           getLunarMonthName(lunarMonth, isLeap), 
           getLunarDayName(lunarDay));
    resetColor();
    
    // 显示节气信息
    char termName[20];
    if (isSolarTerm(today.year, today.month, today.day, termName)) {
        setColor(RED, BLACK);
        printf("\t今天是: %s\n", termName);
        resetColor();
    } else {
        int nextTermDay;
        getNextSolarTerm(today.year, today.month, today.day, termName, &nextTermDay);
        printf("\t下一个节气: %s（%d天后）\n", termName, nextTermDay - today.day);
    }
    
    printf("\n\t1. 查询农历日期\n");
    printf("\t2. 查询公历日期\n");
    printf("\t3. 显示月历\n");
    printf("\t4. 计算日期差(距今天)\n");
    printf("\t5. 计算n天后的日期\n");
    printf("\t6. 计算两日期间隔\n");
    printf("\t7. 显示二十四节气\n");
    printf("\t8. 显示节日\n");
    printf("\t9. 添加事件\n");
    printf("\t10. 查看事件\n");
    printf("\t0. 退出程序\n");
    
    printf("\n请选择功能: ");
}

// 显示月历
void showMonthCalendar(int year, int month) {
    setColor(YELLOW, BLACK);
    printf("\n\t\t%d年%d月\n", year, month);
    resetColor();
    
    // 显示星期标题
    printf("  日  一  二  三  四  五  六\n");
    
    int firstDay = getWeekday(year, month, 1);  // 1号是星期几(0-6)
    int days = getSolarDays(year, month);        // 当月天数
    
    // 打印空格
    for (int i = 0; i < firstDay; i++) printf("    ");
    
    // 打印日期
    for (int day = 1; day <= days; day++) {
        int isWeekend = (firstDay + day - 1) % 7 == 0 || (firstDay + day - 1) % 7 == 6;
        int isTerm = 0;
        char termName[20] = {0};
        int hasEvent = 0;
        
        // 检查是否有事件
        for (int i = 0; i < eventCount; i++) {
            if (events[i].date.year == year && 
                events[i].date.month == month && 
                events[i].date.day == day) {
                hasEvent = 1;
                break;
            }
        }
        
        // 检查是否为节气
        if (isSolarTerm(year, month, day, termName)) {
            isTerm = 1;
        }
        
        // 设置颜色
        if (hasEvent) {
            setColor(RED, BLACK);  // 有事件的日期用红色
        } else if (isTerm) {
            setColor(YELLOW, BLACK);  // 节气用黄色
        } else if (isWeekend) {
            setColor(BLUE, BLACK);  // 周末用蓝色
        }
        
        printf("%4d", day);
        resetColor();
        
        if ((firstDay + day) % 7 == 0) printf("\n");
    }
    printf("\n");
    
    // 显示农历和节气信息
    printf("\n农历信息与节气：\n");
    for (int day = 1; day <= days; day++) {
        int lunarYear, lunarMonth, lunarDay, isLeap;
        solarToLunar(year, month, day, &lunarYear, &lunarMonth, &lunarDay, &isLeap);
        
        char termName[20] = {0};
        if (isSolarTerm(year, month, day, termName)) {
            printf("%d月%d日: %s %s %s 【%s】\n", 
                   month, day, 
                   getChineseYear(lunarYear), 
                   getLunarMonthName(lunarMonth, isLeap), 
                   getLunarDayName(lunarDay),
                   termName);
        }
    }
}

// 显示二十四节气
void showSolarTerms(int year) {
    printf("\n%d年二十四节气：\n", year);
    
    for (int i = 0; i < 24; i++) {
        int month = i / 2 + 1;
        int day = getSolarTermDay(year, i);
        
        printf("%s: %d月%d日\n", solarTerms[i], month, day);
    }
}

// 显示节日
void showHolidays(int year, int month) {
    printf("\n%d年%d月的节日：\n", year, month);
    
    // 显示公历节日
    for (int i = 0; i < 5 && solarHolidays[month][i] != NULL; i += 2) {
        printf("公历: %s - %s\n", solarHolidays[month][i], solarHolidays[month][i+1]);
    }
    
    // 显示农历节日
    for (int i = 0; lunarHolidays[i][0] != NULL; i++) {
        char* holidayDate = (char*)lunarHolidays[i][1];
        int lunarMonth, lunarDay;
        
        // 解析农历日期
        if (strstr(holidayDate, "正月") == holidayDate) {
            lunarMonth = 1;
            sscanf(holidayDate, "正月%d", &lunarDay);
        } else if (strstr(holidayDate, "二月") == holidayDate) {
            lunarMonth = 2;
            sscanf(holidayDate, "二月%d", &lunarDay);
        } else if (strstr(holidayDate, "三月") == holidayDate) {
            lunarMonth = 3;
            sscanf(holidayDate, "三月%d", &lunarDay);
        } else if (strstr(holidayDate, "四月") == holidayDate) {
            lunarMonth = 4;
            sscanf(holidayDate, "四月%d", &lunarDay);
        } else if (strstr(holidayDate, "五月") == holidayDate) {
            lunarMonth = 5;
            sscanf(holidayDate, "五月%d", &lunarDay);
        } else if (strstr(holidayDate, "六月") == holidayDate) {
            lunarMonth = 6;
            sscanf(holidayDate, "六月%d", &lunarDay);
        } else if (strstr(holidayDate, "七月") == holidayDate) {
            lunarMonth = 7;
            sscanf(holidayDate, "七月%d", &lunarDay);
        } else if (strstr(holidayDate, "八月") == holidayDate) {
            lunarMonth = 8;
            sscanf(holidayDate, "八月%d", &lunarDay);
        } else if (strstr(holidayDate, "九月") == holidayDate) {
            lunarMonth = 9;
            sscanf(holidayDate, "九月%d", &lunarDay);
        } else if (strstr(holidayDate, "十月") == holidayDate) {
            lunarMonth = 10;
            sscanf(holidayDate, "十月%d", &lunarDay);
        } else if (strstr(holidayDate, "冬月") == holidayDate) {
            lunarMonth = 11;
            sscanf(holidayDate, "冬月%d", &lunarDay);
        } else if (strstr(holidayDate, "腊月") == holidayDate) {
            lunarMonth = 12;
            sscanf(holidayDate, "腊月%d", &lunarDay);
        } else {
            continue;  // 无法解析的日期格式
        }
        
        // 计算对应的公历日期
        Date solar = lunarToSolar(year, lunarMonth, lunarDay, 0);
        
        // 如果农历节日在指定月份对应的公历月份内
        if (solar.month == month) {
            printf("农历: %s - %d月%d日\n", lunarHolidays[i][0], solar.month, solar.day);
        }
    }
}

int main() {
    SetConsoleOutputCP(65001);
    int choice = -1;
    Date today;
    int year, month, day;
    int days;
    int lunarYear, lunarMonth, lunarDay, isLeap;
    
    while (choice != 0) {
        showMainMenu();
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:  // 查询农历日期
                printf("请输入公历日期（年 月 日）：");
                scanf("%d %d %d", &year, &month, &day);
                solarToLunar(year, month, day, &lunarYear, &lunarMonth, &lunarDay, &isLeap);
                printf("\n%d年%d月%d日对应的农历是：%s %s %s\n", 
                       year, month, day, 
                       getChineseYear(lunarYear), 
                       getLunarMonthName(lunarMonth, isLeap), 
                       getLunarDayName(lunarDay));
                break;
                
            case 2:  // 查询公历日期
                printf("请输入农历日期（年 月 日 是否闰月，0-否，1-是）：");
                scanf("%d %d %d %d", &lunarYear, &lunarMonth, &lunarDay, &isLeap);
                Date solar = lunarToSolar(lunarYear, lunarMonth, lunarDay, isLeap);
                printf("\n%s %s %s对应的公历是：%d年%d月%d日\n", 
                       getChineseYear(lunarYear), 
                       getLunarMonthName(lunarMonth, isLeap), 
                       getLunarDayName(lunarDay),
                       solar.year, solar.month, solar.day);
                break;
                
            case 3:  // 显示月历
                printf("请输入要显示的月份（年 月）：");
                scanf("%d %d", &year, &month);
                showMonthCalendar(year, month);
                break;
                
            case 4:  // 计算日期差(距今天)
                printf("请输入目标日期（年 月 日）：");
                scanf("%d %d %d", &year, &month, &day);
                Date target = {year, month, day, 0};
                getCurTime(&today);
                days = getDaysBetween(today, target);
                if (days > 0) {
                    printf("\n%d年%d月%d日距离今天还有 %d 天\n", year, month, day, days);
                } else if (days < 0) {
                    printf("\n%d年%d月%d日已经过去 %d 天\n", year, month, day, -days);
                } else {
                    printf("\n%d年%d月%d日就是今天！\n", year, month, day);
                }
                break;
                
            case 5:  // 计算n天后的日期
                printf("请输入天数：");
                scanf("%d", &days);
                target = getDateAfterDays(days);
                printf("\n%d天后的日期是：%d年%d月%d日\n", days, target.year, target.month, target.day);
                break;
                
            case 6:  // 计算两日期间隔
                printf("请输入第一个日期（年 月 日）：");
                scanf("%d %d %d", &year, &month, &day);
                Date date1 = {year, month, day, 0};
                
                printf("请输入第二个日期（年 月 日）：");
                scanf("%d %d %d", &year, &month, &day);
                Date date2 = {year, month, day, 0};
                
                days = getDaysBetween(date1, date2);
                printf("\n%d年%d月%d日 到 %d年%d月%d日 间隔 %d 天\n", 
                       date1.year, date1.month, date1.day,
                       date2.year, date2.month, date2.day,
                       days);
                break;
                
            case 7:  // 显示二十四节气
                printf("请输入年份：");
                scanf("%d", &year);
                showSolarTerms(year);
                break;
                
            case 8:  // 显示节日
                printf("请输入年份和月份（年 月）：");
                scanf("%d %d", &year, &month);
                showHolidays(year, month);
                break;
                
            case 9:  // 添加事件
                addEvent();
                break;
                
            case 10:  // 查看事件
                viewEvents();
                break;
                
            case 0:  // 退出程序
                printf("感谢使用超级万年历，再见！\n");
                break;
                
            default:
                printf("无效选择，请重新输入！\n");
        }
        
        if (choice != 0) {
            printf("\n按Enter键继续...");
            while (getchar() != '\n');  // 清除输入缓冲区
            getchar();  // 等待用户按Enter
        }
    }
    
    return 0;
}    